package cn.bugstack.mybatis.session;

import cn.bugstack.mybatis.bindings.MapperRegistry;
import cn.bugstack.mybatis.mapping.MappedStatement;
import cn.bugstack.mybatis.session.defaults.DefaultSqlSession;

import java.util.HashMap;
import java.util.Map;

public class Configuration {

    /**
     * store MappedStatement parsed from xmlConfigBuilder
     */
    private final Map<String, MappedStatement> mappedStatements = new HashMap<>();

    /**
     * MapperRegistry 和 Configuration 互相引用
     */
    private MapperRegistry mapperRegistry = new MapperRegistry(this);


    public void addMappedStatement(MappedStatement mappedStatement) {
        mappedStatements.put(mappedStatement.getId(), mappedStatement);
    }

    public MappedStatement getMappedStatement(MappedStatement mappedStatement) {
        return mappedStatements.get(mappedStatement.getId());
    }

    public void addMapper(Class<?> mapperInterface) {
        mapperRegistry.addMapper(mapperInterface);
    }

    public <T> T getMapper(Class<T> type, DefaultSqlSession defaultSqlSession) {
        return mapperRegistry.getMapper(type, defaultSqlSession);
    }


    public MappedStatement getMappedStatement(String statementName) {
        return mappedStatements.get(statementName);
    }
}
